package com.hmall.pay;

import com.alibaba.fastjson.JSON;
import com.hmall.common.client.ItemClient;
import com.hmall.common.dto.Item;
import com.hmall.pay.pojo.ItemDoc;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
@SpringBootTest
public class asd {
    @Autowired
    private ItemClient itemClient;

    private RestHighLevelClient client;
    @BeforeEach
    void setUp() {
        this.client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.209.130:9200")
        ));
    }
    @Test
    void testAddDocument() throws IOException {
        // 1.根据id查询酒店数据
        Item item = itemClient.getItemByid("317578");
        ItemDoc itemDoc = new ItemDoc(item);
        String json = JSON.toJSONString(itemDoc);
        //String json = JSON.toJSONString(item);
        IndexRequest request = new IndexRequest("item").id(item.getId().toString());
        // 2.准备Json文档
        request.source(json, XContentType.JSON);
        // 3.发送请求
        client.index(request, RequestOptions.DEFAULT);
    }
    @AfterEach
    void tearDown() throws IOException {
        this.client.close();
    }
}
